import {observable, action, toJS} from 'mobx';
import * as API from '../apis/monitor';
import {Modal, message} from 'antd';
import { WeaTableNew, WeaForm } from 'comsMobx';
const {TableStore} = WeaTableNew;

import {WeaTools,WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export default class Monitor {
  @observable monitorTableStore = new TableStore();//服务类型
  @observable monitorForm = new WeaForm();
  @observable sessionKey = '';
  @observable status = {
    conditioninfo : [],
    isAlertPage: false,
    timeSag: '0',
    showSearchAd: false,
    isInit: false,
    rowSelectIds: [],
    previewVisible: false, //查看页面显示隐藏
    previewId: '', //查看id
    delLoading: false,
  }

  doInit = () => {
    this.status.timeSag='0';
    this.getMonitor({timeSag: '0'}, true);
    this.getCondition();
  }

  setState = (params = {}) => {
    let status = {...this.status};
    Object.keys(params).forEach(key => {
      status[key] = params[key];
    });
    this.status = status;
  }

  //信息确认
  doConfirm = (content, func, val) => {//确认信息， 方法， 方法参数
    Modal.confirm({
      title: `${getLabel(131329,'信息确认')}`,
      content: content,
      okText: `${getLabel(382958,'确定')}`,
      cancelText:`${getLabel(31129,'取消')}`,
      onOk: () => func(val),
    })
  }

  setModal = (content) => {
    Modal.warning({
      title: `${getLabel(15172,'系统提示')}`,
      content: content,
    });
  }

  // 列表数据
  getMonitor = (isOnChange = false, param) => {
    let params = param || this.monitorForm.getFormParams();
    API.getMonitor(params).then(result => {
      if (result.sessionkey) {
        isOnChange ? this.monitorTableStore.getDatas(result.sessionkey, 1) : this.monitorTableStore.getDatas(result.sessionkey)
        this.sessionKey = result.sessionkey
      } else {
        this.sessionKey = '';
        this.status = {...this.status, isAlertPage: true}
      }
    })
  }

  // 高级搜索条件
  getCondition = () => {
    API.getCondition().then(result => {
      this.monitorForm.initFormFields(result.condition);
      this.status = {...this.status, conditioninfo: result.condition}
    })
  }

  // 提前结束
  monitorOver = (params) => {
    API.over(params).then(result => {
      if(result.ret == 'true'){
        message.success(`${getLabel(30700,'操作成功')}`);
        this.getMonitor();
      }else {
        message.error(`${getLabel(30651,'操作失败')}`);
      }
    })
  }

  // 取消会议
  monitorCancel = (params) => {
    API.cancel(params).then(result => {
      if(result.ret == 'true'){
        message.success(`${getLabel(30700,'操作成功')}`);
        this.getMonitor();
      }else {
        message.error(`${getLabel(30651,'操作失败')}`);
      }
    })
  }

  // 删除
  monitorDelete = (params) => {
    this.status.delLoading = true;
    API.monitorDelete(params).then(result => {
      this.status.delLoading = false;
      if(result.ret == 'true'){
        message.success(`${getLabel(20461,'删除成功')}`);
        this.getMonitor();
      }else {
        message.error(`${getLabel(20462,'删除失败')}`);
      }
    })
  }

  exportAll = ()=>{
    WeaTools.callApi('/api/ec/dev/table/counts', 'POST', {dataKey:this.sessionKey}).then((result)=>{
      if(result.status){
        if(result.count > 10000){
          this.setModal(getLabel(508434,'目前支持的最大导出数据量为10000条,请修改限制条件,勿超过此数量。'))
        }else{
          this.monitorTableStore.exportAll();
        }
      }
    })
  }

}